<meta title="免单订单"/>
<div class="p">
    <div class="m-b clearfix">
        <form class="form-inline pull-left" id="form">
            <input autocomplete="off" type="text" class="form-control input-sm" placeholder="日期"
                   name="date" s="datetimepicker"/>
            <select class="form-control input-sm" s="select"
                    s-build="flashSaleSettingSeasonList()"
                    s-select="{title: 'title', value: 'time'}"
                    name="season">
                <option value="">选择时段</option>
            </select>
            <input autocomplete="off" type="text" class="form-control input-sm" placeholder="用户ID"
                   name="userId"/>
            <input autocomplete="off" type="text" class="form-control input-sm" placeholder="商品ID" style="width:200px;"
                   name="itemId"/>
            <input autocomplete="off" type="text" class="form-control input-sm" placeholder="电商平台订单号"
                   style="width:200px;" name="orderNo"/>
            <input autocomplete="off" type="text" class="form-control input-sm" placeholder="创建开始时间"
                   style="width:120px;"
                   s="datetimepicker" name="minCreateTime"/> -
            <input autocomplete="off" type="text" class="form-control input-sm" placeholder="创建结束时间" s="datetimepicker"
                   style="width:120px;" name="maxCreateTime"/>
            <input autocomplete="off" type="hidden" name="page" id="page" value="1"/>
            <select class="form-control input-sm" s="select"
                    s-build="S.get('/api/enum/valuesMap/core.enums.FreeChargeActivityOrderStatus')"
                    name="status">
                <option value="">状态</option>
            </select>
            <button type="button" class="btn btn-default btn-sm"
                    s-click="$page.node.val(1); $flashSaleOrdersGrid.refresh()">查询</button>
        </form>
        <div class="pull-right">
            <a href="javascript:;" s-click="settle()" class="btn btn-warning btn-outline btn-sm">结算</a>
        </div>
    </div>
    <div id="flashSaleOrdersGrid"
         s='datac,nda,check'
         s-data="S.get('/api/flash-sale-order/search', $form.node.serialize())">
        <table class="table table-striped table-bordered table-hover table-middle">
            <tr>
                <th>
                    <label class="s-checkbox">
                        <input autocomplete="off" type="checkbox" s-check-role="checkall-h"/>
                        <span class="s-checkbox-indicator"></span>
                    </label>
                </th>
                <th>期数</th>
                <th>商品</th>
                <th>会员</th>
                <th>订单编号</th>
                <th>免单金额</th>
                <th>创建时间</th>
                <th>更新时间</th>
                <th>状态</th>
                <th class="text-right">操作</th>
            </tr>
            <tbody s="loop" s-data-filter="'results'">
            <tr s-loop-role="empty">
                <td colspan="10">暂无数据</td>
            </tr>
            <tr s-loop-role="row" s-check-role="i" s="datac" class="flashSaleOrder">
                <td>
                    <label class="s-checkbox">
                        <input autocomplete="off" type="checkbox" s-check-role="h"/>
                        <span class="s-checkbox-indicator"></span>
                    </label>
                </td>
                <td>
                    <span name="flashSaleItemDate"></span> -
                    <span name="flashSaleItemSeason"></span>
                </td>
                <td s="tpl" class="row">
                    <div style="width: 200px;">
                        <img src="{%=this.itemPicUrl%}" style="max-width: 50px; max-height: 50px;" class="pull-left"/>
                        <div class="pull-right" style="width: 140px;">
                            <div style="text-overflow: ellipsis; overflow: hidden; height: 40px;">{%=this.itemTitle%}</div>
                            <div style="text-overflow: ellipsis; overflow: hidden; height: 40px;">ID:{%=this.itemId%}</div>
                        </div>
                    </div>
                </td>
                <td s="tpl">
                    <div style="width: 200px;">
                        <img src="{%=this.userAvatarUrl%}" style="max-width: 50px; max-height: 50px;"
                             class="pull-left"/>
                        <div class="pull-right" style="width: 140px;">
                            <div>ID:{%=this.userId%}</div>
                            <div>{%=this.userNickname%}</div>
                            <div class="text-warning">{%=this.userPhone%}</div>
                        </div>
                    </div>
                </td>
                <td name="orderNo"></td>
                <td name="freeFee"></td>
                <td name="createTime"></td>
                <td name="updateTime"></td>
                <td s="tpl">
                    <label class="label
                            {%
                                switch(this.status) {
                                    case 'WAIT_VERIFY': out.print('label-default'); break;
                                    case 'WAIT_RECEIVE_GOODS': out.print('label-info'); break;
                                    case 'WAIT_SETTLE': out.print('label-warning'); break;
                                    case 'SETTLED': out.print('label-success'); break;
                                    case 'INVALID': out.print('label-default'); break;
                                }
                            %}
                            ">{%=this.statusTitle%}</label>
                </td>
                <td class="text-right" s="tpl">
                    {%if(this.status == 'WAIT_SETTLE') {%}
                    <a href="javascript:;" s-click="settleOrder(this)" class="btn btn-link btn-xs">结算</a>
                    {%}%}
                    {%if(this.status == 'WAIT_VERIFY') {%}
                    <a href="javascript:;" s-click="del(this)" class="btn btn-link btn-xs">删除</a>
                    {%}%}
                    <a href="javascript:;" class="btn btn-info btn-xs btn-outline" s-click="doRemind(this)">提醒</a>
                </td>
            </tr>
            </tbody>
            <tfoot>
            <tr>
                <td colspan="10" class="text-right">
                    共有<span name="total"></span>个数据
                    <ul s="pagination"
                        s-pagination="{action:function(){$page.node.val(this); $flashSaleOrdersGrid.refresh()}}"
                        class="pagination pagination-sm">
                    </ul>
                </td>
            </tr>
            </tfoot>
        </table>
    </div>
</div>
<script type="text/javascript">

    function settle() {
        var datas = $flashSaleOrdersGrid.getCheckedData('id');
        if (datas.length) {
            settleByIds(datas)
        }
    }

    function settleOrder($this) {
        var data = $this.parent().data();
        settleByIds([data.id])
    }

    function del($this) {
        S.confirm("确认进行删除吗？请输入OK进行确认", {
            confirmInput: 'OK'
        }).done(function() {
            var data = $this.parent().data();
            S.remove("/api/flash-sale-order/" + data.id).done(function() {
                $flashSaleOrdersGrid.refresh()
            })
        })
    }

		function doRemind($this) {
			S.confirm("确定操作吗？").done(function () {
				var data =  $this.parent().data();
				S.put("/api/flashSaleItem/remind/" + data.flashSaleItemId, { userId: data.userId }).done(function () {
					S.toast('操作成功', 'success')
				})
			})
		}

    function settleByIds(ids) {
        S.confirm("确认进行结算吗？请输入OK进行确认", {
            confirmInput: 'OK'
        }).done(function() {
            S.post("/api/flash-sale-order/settle", {idList: ids}).done(function() {
                $flashSaleOrdersGrid.refresh()
            })
        })
    }

    function flashSaleSettingSeasonList() {
    	var deferred = $.Deferred()
      S.get('/api/setting/FlashSaleSetting').done(function(rs) {
      	console.info(rs.seasonList)
        deferred.resolve(rs.seasonList)
      })
      return deferred;
    }

</script>